home *** CD-ROM | disk | FTP | other *** search
/ isnet Internet / Isnet Internet CD.iso / prog / hiz / 09 / 09.exe / adynware.exe / perl / lib / site / LWP / Simple.pm < prev    next >
Encoding:
Perl POD Document  |  1999-12-28  |  5.3 KB  |  225 lines

  1.  
  2. =head1 NAME
  3.  
  4. get, head, getprint, getstore, mirror - Procedural LWP interface
  5.  
  6. =head1 SYNOPSIS
  7.  
  8.  perl -MLWP::Simple -e 'getprint "http://www.sn.no"'
  9.  
  10.  use LWP::Simple;
  11.  $content = get("http://www.sn.no/")
  12.  if (mirror("http://www.sn.no/", "foo") == RC_NOT_MODIFIED) {
  13.      ...
  14.  }
  15.  if (is_success(getprint("http://www.sn.no/"))) {
  16.      ...
  17.  }
  18.  
  19. =head1 DESCRIPTION
  20.  
  21. This interface is intended for those who want a simplified view of the
  22. libwww-perl library.  This interface should also be suitable for
  23. one-liners.  If you need more control or access to the header fields
  24. in the requests sent and responses received you should use the full OO
  25. interface provided by the LWP::UserAgent module.
  26.  
  27. This following functions are provided (and exported) by this module:
  28.  
  29. =over 3
  30.  
  31. =item get($url)
  32.  
  33. This function will get the document identified by the given URL.  The
  34. get() function will return the document if successful or 'undef' if it
  35. fails.  The $url argument can be either a simple string or a reference
  36. to a URI::URL object.
  37.  
  38. You will not be able to examine the response code or response headers
  39. (like I<Content-Type>) when you are accessing the web using this
  40. function.  If you need this you should use the full OO interface.
  41.  
  42. =item head($url)
  43.  
  44. Get document headers. Returns the following values if successful:
  45. ($content_type, $document_length, $modified_time, $expires, $server)
  46.  
  47. Returns an empty list if it fails.
  48.  
  49. =item getprint($url)
  50.  
  51. Get and print a document identified by a URL. The document is printet
  52. on STDOUT. The error message (formatted as HTML) is printed on STDERR
  53. if the request fails.  The return value is the HTTP response code.
  54.  
  55. =item getstore($url, $file)
  56.  
  57. Gets a document identified by a URL and stores it in the file. The
  58. return value is the HTTP response code.
  59.  
  60. =item mirror($url, $file)
  61.  
  62. Get and store a document identified by a URL, using
  63. I<If-modified-since>, and checking of the I<Content-Length>.  Returns
  64. the HTTP response code.
  65.  
  66. =back
  67.  
  68. This module also exports the HTTP::Status constants and
  69. procedures.  These can be used when you check the response code from
  70. getprint(), getstore() and mirror().  The constants are:
  71.  
  72.    RC_CONTINUE
  73.    RC_SWITCHING_PROTOCOLS
  74.    RC_OK
  75.    RC_CREATED
  76.    RC_ACCEPTED
  77.    RC_NON_AUTHORITATIVE_INFORMATION
  78.    RC_NO_CONTENT
  79.    RC_RESET_CONTENT
  80.    RC_PARTIAL_CONTENT
  81.    RC_MULTIPLE_CHOICES
  82.    RC_MOVED_PERMANENTLY
  83.    RC_MOVED_TEMPORARILY
  84.    RC_SEE_OTHER
  85.    RC_NOT_MODIFIED
  86.    RC_USE_PROXY
  87.    RC_BAD_REQUEST
  88.    RC_UNAUTHORIZED
  89.    RC_PAYMENT_REQUIRED
  90.    RC_FORBIDDEN
  91.    RC_NOT_FOUND
  92.    RC_METHOD_NOT_ALLOWED
  93.    RC_NOT_ACCEPTABLE
  94.    RC_PROXY_AUTHENTICATION_REQUIRED
  95.    RC_REQUEST_TIMEOUT
  96.    RC_CONFLICT
  97.    RC_GONE
  98.    RC_LENGTH_REQUIRED
  99.    RC_PRECONDITION_FAILED
  100.    RC_REQUEST_ENTITY_TOO_LARGE
  101.    RC_REQUEST_URI_TOO_LARGE
  102.    RC_UNSUPPORTED_MEDIA_TYPE
  103.    RC_INTERNAL_SERVER_ERROR
  104.    RC_NOT_IMPLEMENTED
  105.    RC_BAD_GATEWAY
  106.    RC_SERVICE_UNAVAILABLE
  107.    RC_GATEWAY_TIMEOUT
  108.    RC_HTTP_VERSION_NOT_SUPPORTED
  109.  
  110. The HTTP::Status classification functions are:
  111.  
  112. =over 3
  113.  
  114. =item is_success($rc)
  115.  
  116. Check if response code indicated successfull request.
  117.  
  118. =item is_error($rc)
  119.  
  120. Check if response code indicated that an error occured.
  121.  
  122. =back
  123.  
  124. The module will also export the LWP::UserAgent object as C<$ua> if you
  125. ask for it explicitly.
  126.  
  127. The user agent created by this module will identify itself as
  128. "LWP::Simple/0.00" and will initialize its proxy defaults from the
  129. environment (by calling $ua->env_proxy).
  130.  
  131. =head1 SEE ALSO
  132.  
  133. L<LWP>, L<LWP::UserAgent>, L<HTTP::Status>, L<request>, L<mirror>
  134.  
  135. =cut
  136.  
  137.  
  138. package LWP::Simple;
  139.  
  140. require Exporter;
  141. @ISA = qw(Exporter);
  142. @EXPORT = qw(get head getprint getstore mirror);  # note additions below
  143. @EXPORT_OK = qw($ua);
  144.  
  145. use HTTP::Status;
  146. push(@EXPORT, @HTTP::Status::EXPORT);
  147.  
  148. require LWP;
  149. require LWP::UserAgent;
  150. use HTTP::Date qw(str2time);
  151. use Carp;
  152.  
  153. $ua = new LWP::UserAgent;  # we create a global UserAgent object
  154. $ua->agent("LWP::Simple/$LWP::VERSION");
  155. $ua->env_proxy;
  156.  
  157.  
  158. sub get ($)
  159. {
  160.     my($url) = @_;
  161.  
  162.     my $request = new HTTP::Request 'GET', $url;
  163.     my $response = $ua->request($request);
  164.  
  165.     return $response->content if $response->is_success;
  166.     return undef;
  167. }
  168.  
  169.  
  170. sub head ($)
  171. {
  172.     my($url) = @_;
  173.  
  174.     my $request = new HTTP::Request HEAD => $url;
  175.     my $response = $ua->request($request);
  176.  
  177.     if ($response->is_success) {
  178.     return $response unless wantarray;
  179.     return ($response->header('Content-Type'),
  180.         $response->header('Content-Length'),
  181.         str2time($response->header('Last-Modified')),
  182.         str2time($response->header('Expires')),
  183.         $response->header('Server'),
  184.            );
  185.     } else {
  186.     return wantarray ? () : '';
  187.     }
  188. }
  189.  
  190.  
  191. sub getprint ($)
  192. {
  193.     my($url) = @_;
  194.  
  195.     my $request = new HTTP::Request 'GET', $url;
  196.     my $response = $ua->request($request);
  197.     local($\) = ""; # ensure standard $OUTPUT_RECORD_SEPARATOR
  198.     if ($response->is_success) {
  199.     print $response->content;
  200.     } else {
  201.     print STDERR $response->error_as_HTML;
  202.     }
  203.     $response->code;
  204. }
  205.  
  206.  
  207. sub getstore ($$)
  208. {
  209.     my($url, $file) = @_;
  210.  
  211.     my $request = new HTTP::Request 'GET', $url;
  212.     my $response = $ua->request($request, $file);
  213.  
  214.     $response->code;
  215. }
  216.  
  217. sub mirror ($$)
  218. {
  219.     my($url, $file) = @_;
  220.     my $response = $ua->mirror($url, $file);
  221.     $response->code;
  222. }
  223.  
  224. 1;
  225.